package com.movie.controller;

import com.movie.dao.UserDao;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;

public class RegisterController extends HttpServlet {
    private UserDao userDao;

    @Override
    public void init() throws ServletException {
        userDao = new UserDao();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 跳转到注册页面
        request.getRequestDispatcher("register.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        // 检查用户名是否已被注册
        if (userDao.registerUser(username, password)) {
            response.sendRedirect("login.jsp"); // 注册成功，跳转到登录页面
        } else {
            request.setAttribute("error", "用户名已存在或注册失败");
            request.getRequestDispatcher("register.jsp").forward(request, response);
        }
    }
}
